<?php
namespace app\service;

use Swoole\Http\Response as SwooleResponse;

class Response
{

    /**
     * @var SwooleResponse
     */
    protected $swooleResponse;

    public function __construct(SwooleResponse $response)
    {
        $this->swooleResponse = $response;
    }

    /**
     * getSwooleResponse
     * @return SwooleResponse
     */
    public function getSwooleResponse()
    {
        return $this->swooleResponse;
    }

    /**
     * redirect
     * @param string $location
     * @param null   $http_code
     * @return mixed
     */
    public function redirect(string $location, $http_code = null)
    {
        return $this->swooleResponse->redirect($location, $http_code);
    }

    /**
     * cookie
     * @param string $key
     * @param string $value
     * @param int    $expire
     * @param string $path
     * @param string $domain
     * @param bool   $secure
     * @param bool   $httponly
     * @param string $samesite
     * @param string $priority
     * @return mixed
     */
    public function cookie(string $key, string $value = '', int $expire = 0 , string $path = '/', string $domain  = '', bool $secure = false , bool $httponly = false, string $samesite = '', string $priority = '')
    {
        return $this->swooleResponse->cookie($key, $value, $expire, $path, $domain, $secure, $httponly , $samesite, $priority);
    }

    /**
     * asJson
     * @param array $returnData
     * @param int   $options
     * @return mixed
     */
    public function asJson(array $returnData, int $options = 320)
    {
        $this->swooleResponse->header('Content-Type', 'application/json; charset=utf-8');

        $this->swooleResponse->status(200);

        return $this->swooleResponse->end(json_encode($returnData,$options));
    }

    /**
     * asHtml
     * @param null $content
     * @return mixed
     */
    public function asHtml($content = null)
    {
        $this->swooleResponse->status(200);

        return $this->swooleResponse->end($content);
    }

}